package com.hup.timer.view.customView;

import com.hup.timer.model.TimeViewConfig;
import com.hup.timer.model.TimerConfig;
import com.hup.timer.mvp.MainMvp.MainPresenter;
import com.hup.timer.service.gesture.SimpleActionListener;
import com.hup.timer.view.TimerWindow;
import com.hup.utils.commons.hardware.Beeper;
import com.hup.utils.commons.hardware.MouseUtil;
import com.hup.utils.commons.start.DialogHelper;
import com.hup.utils.commons.start.SpringContext;
import com.hup.utils.commons.thread.TimeCatStarter;
import com.hup.utils.commons.thread.TimeCatStarter.StarterHandler;
import com.hup.utils.swing.ComponentUtil;
import com.hup.utils.swing.DimensionUtil;
import com.hup.utils.swing.FrameUtil;
import com.hup.utils.swing.FrameUtil.VisibleListener;
import lombok.extern.log4j.Log4j2;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.text.SimpleDateFormat;

/**
 * @author hugan
 * @date 2020/4/4
 */
@Log4j2
public class TimeView extends JLabel {

    private final TimerConfig config;
    private SimpleDateFormat DF_A;
    private SimpleDateFormat DF_B;
    private boolean is12H;

    private final TimeCatStarter timeCat;

    public TimeView(TimerWindow timerWindow) {
        super((String) null, SwingConstants.CENTER);
        setBorder(new EmptyBorder(0, 0, 0, 0));
        setOpaque(true);
        updateFormat(true);
        setText(DF_A.format(System.currentTimeMillis()));

        MainPresenter presenter = SpringContext.getBean(MainPresenter.class);
        config = presenter.getConfig();

        timeCat = new TimeCatStarter(false);
        timeCat.start(this, new StarterHandler() {
            @Override
            public void run(TimeCatStarter starter) {
                starter.perSecond(() -> {
                    boolean useA = System.currentTimeMillis() / 1000 % 2 == 0;
                    //log.info("updateTime={}", useA);
                    String text = (useA ? DF_A : DF_B).format(System.currentTimeMillis());
                    setText(text);

                    if (config.mousePenetration) {
                        //解决 鼠标穿透时,接收不到mouseEntered
                        Point mouseLoc = MouseUtil.getLocation();
                        boolean contains = timerWindow.getBounds().contains(mouseLoc);
                        //log.info("contains={}", contains);
                        FrameUtil.setAlpha(timerWindow, contains ? SimpleActionListener.ALPHA_MOUSE_ENTRY : config.timeViewConfig.getAlpha());
                    }
                });
            }

            @Override
            public void onException(Exception e) {
                SpringContext.getBean(Beeper.class).beep();
                DialogHelper.show("刷新时间错误:", e, ComponentUtil.getParentWindow(TimeView.this));
            }
        });

        FrameUtil.addVisibleListener(timerWindow, new VisibleListener() {
            @Override
            public void onShown() {
                timeCat.doResume();
            }

            @Override
            public void onHidden(boolean dispose) {
                timeCat.doPause();
            }
        });
    }

    private void updateFormat(boolean is12H) {
        this.is12H = is12H;
        DF_A = new SimpleDateFormat(is12H ? "hh:mm" : "HH:mm");
        DF_B = new SimpleDateFormat(is12H ? "hh mm" : "HH mm");
    }

    public void applyConfig(TimeViewConfig config) {
        setForeground(config.getFontColor());
        setBackground(config.getBgColor());

        Font font = new Font("黑体", Font.BOLD, config.fontSize);
        setFont(font);

        if (is12H != config.is12H) {
            log.debug("切换12小时制,is12H={}", config.is12H);
            updateFormat(config.is12H);
        }
    }

    /**
     * 根据文本返回view尺寸
     */
    public Dimension getViewSize() {
        return getViewSize(getFont());
    }

    /**
     * 根据文本返回view尺寸
     */
    public Dimension getViewSize(Font font) {
        Dimension textSize = DimensionUtil.getSizeOfNumber(font, getText());
        int height = textSize.height;
        int width = textSize.width;
        if (height > 60) {
            int padding = 5;
            width += padding * 2;
            height += padding * 2;
        } else {
            width += 1;
        }
        textSize.height = height;
        textSize.width = width;
        return textSize;
    }

}
